﻿using System;
using System.Text;
using Imps.Client.Core;
using Imps.Client.Utils;
using Imps.Common;
using Imps.Utils;
using com.hetaoos.FetionSDK.Event;

namespace com.hetaoos.FetionSDK.Account
{
    public class Login
    {
        private MainPresence _initPresence = MainPresence.Invisible;
        private UserAccountManager _manager;
        private bool isLogining;
        private UserAccounts.AccountData currentAd;
        private string userid;
        private string pwdhash;

        public Login(UserAccountManager manager)
        {
            this._manager = manager;
        }

        internal void CancelLogin()
        {
            try
            {
                this.UpdateStepLabelText("取消中...");
            }
            catch
            {
            }
            try
            {
                this._manager.CurrentUser.AsyncCancelLogin();
                isLogining = false;
            }
            catch (Exception exception)
            {
                this._manager.FrameworkWnd.UnifiedMessageBox.ShowException(exception);
            }
        }

        internal bool CheckInput(out long id, out string password)
        {
            id = 0L;
            password = pwdhash;
            if (userid.Length <= 0)
            {
                //this.ShowErrorTip(StringTable.User.MsgEmptyId, this.cmbId);
                return false;
            }
            char ch = userid[0];
            bool flag = ((ch < '1') || (ch > '9')) || !long.TryParse(userid, out id);
            bool flag2 = password.Length <= 0;
            bool flag3 = ImpsHelper.IsSid(id);
            bool flag4 = ImpsHelper.IsCmccMobileNo(id);
            if ((flag || (id <= 0L)) || (!flag3 && !flag4))
            {
                //this.ShowErrorTip(StringTable.User.MsgInvalidId, this.cmbId);
                return false;
            }
            if (flag2)
            {
                //this.ShowErrorTip(StringTable.User.MsgInvalidPassword, this.tbPassword);
                return false;
            }
            return true;
        }



        internal bool DoLogin()
        {
            return this.DoLogin(false);
        }

        private bool DoLogin(bool auto)
        {
            try
            {
                long num;
                string str;
                if (!this.CheckInput(out num, out str))
                {
                    return false;
                }

                if (this._initPresence == MainPresence.OfflineLogin)
                {
                    try
                    {
                        try
                        {
                            this.currentAd = UserAccounts.GetAccount(num.ToString());
                            if (this.currentAd == null)
                            {
                                UpdateStepLabelText("请连接服务器，验证帐户信息后，才能脱机登录！");
                                return false;
                            }
                            if (!string.Equals(str, this.currentAd.Password))
                            {
                                UpdateStepLabelText("帐号或密码错误！");
                                return false;
                            }
                        }
                        catch
                        {
                            return false;
                        }
                    }
                    finally
                    {
                        this.currentAd = null;
                    }
                }
                this._manager.LoginAs(num, str, this._initPresence, auto);
                isLogining = true;
                return true;
            }
            catch (Exception exception)
            {
                this._manager.FrameworkWnd.UnifiedMessageBox.ShowException(exception);
                ClientLogger.WriteException(exception);
                return false;
            }
        }

        private void DoLoginOrCancel()
        {
            if (isLogining==false)
            {
                this.UpdateStepLabelText("正在登录...");
                this.DoLogin();

            }
            else
            {
                this.UpdateStepLabelText("正在取消登录...");
                this.CancelLogin();
            }
        }

        internal void FillUserIdAndPassword(string id, string pass)
        {
            if (!FuncLimitedSetting.SSIEncryptLoginLimited)
            {
                userid = id;
                pwdhash = this._manager.CurrentUser.HashPassword(Encoding.UTF8.GetBytes(pass));
            }
            else
            {
                userid = id;
                pwdhash = pass;
            }
        }


        //private void ShowErrorTip(string text, Control ctrl)
        //{
        //    if (ctrl is TextBoxBase)
        //    {
        //        ((TextBoxBase)ctrl).SelectAll();
        //    }
        //    else if (ctrl is ComboBox)
        //    {
        //        ((ComboBox)ctrl).SelectAll();
        //    }
        //    ctrl.Focus();
        //    BalloonHelper.ShowBallon(ctrl, text, StringTable.User.MsgErrorTitle, ToolTipIcon.Error, new Point(10, 5), 0x7d0);
        //}

        private void UpdateStepLabelText(string newLabelText)
        {
            this._manager.FrameworkWnd.doSDK_Error(this,ErrorType.Login,newLabelText);
        }

        public string CurrentId
        {
            get
            {
                return userid ;
            }
            set
            {
                if (value != string.Empty)
                {
                    userid = value;
                }
            }
        }

    }
}
